//
//  FanViewController.m
//  selfLocation
//
//  Created by h92 on 15/1/7.
//  Copyright (c) 2015年 李腾飞. All rights reserved.
//

#import "FanViewController.h"

@interface FanViewController ()<CLLocationManagerDelegate>

@end

@implementation FanViewController
@synthesize locationManager;
@synthesize latitude;
@synthesize longtitude;
@synthesize altitude;
@synthesize textLabel;
@synthesize currentLocation;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [locationManager startUpdatingLocation];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [locationManager stopUpdatingLocation];
}

/*---------------------------初始化的方法----------------------------------*/
-(void)initView
{
    textLabel.text=@"今天我生日";
    currentLocation=[[CLLocation alloc]init];
    
    locationManager=[[CLLocationManager alloc]init];
    //   locationManager=[CLLocationManager new];
    [locationManager requestAlwaysAuthorization ];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    
    //authorization 授权，认可
    locationManager.distanceFilter=1000.0f;
    if ([CLLocationManager locationServicesEnabled]==NO)
    {
        NSLog(@"设备不可用");
    }
}
/*---------------------------实现代理方法----------------------------------*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"刷新加载location方法执行");
    currentLocation=[locations lastObject];
    longtitude.text=[NSString stringWithFormat:@"%3.5f",currentLocation.coordinate.longitude];
    latitude.text=[NSString stringWithFormat:@"%3.5f",currentLocation.coordinate.latitude];
    altitude.text=[NSString stringWithFormat:@"%3.5f",currentLocation.altitude];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // deny 否定 拒绝
    if (error.code==kCLErrorDenied) {
        NSLog(@"请求被拒绝");
    }else if (error.code==kCLErrorLocationUnknown)
    {
        NSLog(@"不知道哪里出错了");
    }
}

/*---------------------------点击事件----------------------------------*/
- (IBAction)pressDoing:(id)sender {
    // 小译：geo :海湾，地球土地   geocoder:地理编码
    CLGeocoder *geocoder =[[CLGeocoder alloc]init];
    
        /*------------------------------reverseGeocodeLocation 函数-------------------------------*/
    
        // 小译：reverse 背面的反面的  completion：完成结束  handler:处理者，管理者
        // reverse geocode requests
        // 小译：-（）位置的反地理信息编码：某位置  完成处理的程序：处理
        //- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
    
    
        /*------------------------------reverseGeocodeLocation 函数-------------------------------*/
        // 接下来看
        // typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
        // block的定义两种形式
        // http://blog.csdn.net/enuola/article/details/8674063
    
    [geocoder reverseGeocodeLocation:(CLLocation *)currentLocation completionHandler:^(NSArray *placemarks, NSError *error){
                if([placemarks count]>0)
                {
                    CLPlacemark *placemark=placemarks[0];//placemarks 数组类型，存储ClPlacemark对象
                    NSDictionary *addressDictionary=placemark.addressDictionary;//获取了地址字典信息
                    
                    NSString *address = [addressDictionary
                                         objectForKey:(NSString *)kABPersonAddressStreetKey];
                    
                    address = address == nil ? @"": address;
                    
                    NSString *state = [addressDictionary
                                       objectForKey:(NSString *)kABPersonAddressStateKey];
                    state = state == nil ? @"": state;
                    
                    NSString *city = [addressDictionary
                                      objectForKey:(NSString *)kABPersonAddressCityKey];
                    // 表达式
                    city = city == nil ? @"": city;
                    
                   textLabel.text = [NSString stringWithFormat:@"%@ \n%@ \n%@",state, address,city];
//                    textLabel.text=[NSString stringWithFormat:@"%@",state];
                }
        
        }];
    
    
    
}
@end










